﻿@page "/deferredcreds/{id}"
@inject IState<DeferredCredentialState> deferredCredentialsState
@inject IDispatcher dispatcher
@inject NotificationService notificationService
@inject NavigationManager navigationManager
@inject IOptions<RequestLocalizationOptions> requestLocalizationOptions
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@using Microsoft.AspNetCore.Builder
@using Microsoft.Extensions.Options
@using SimpleIdServer.CredentialIssuer.Domains;
@using SimpleIdServer.CredentialIssuer.Website.Stores.CredentialIssuer;
@using SimpleIdServer.CredentialIssuer.Website.Shared;
@using System.Globalization;

<RadzenBreadCrumb>
    <RadzenBreadCrumbItem Path="/deferredcreds" Text="@Global.DeferredCredentialsTitle" />
    <RadzenBreadCrumbItem Text="@Global.DeferredCredentialTitle" />
</RadzenBreadCrumb>

@if (deferredCredentialsState.Value.DeferredCredential.Configuration != null)
{
    <RadzenText class="mt-3 mb-3" Text="@(deferredCredentialsState.Value.DeferredCredential.Configuration.Type)" TextStyle="TextStyle.DisplayH3" />
    <div class="mb-1">
        <RadzenBadge Text="@deferredCredentialsState.Value.DeferredCredential.Configuration.Format" IsPill="true" />
        <RadzenBadge Text="@Enum.GetName(typeof(DeferredCredentialStatus), deferredCredentialsState.Value.DeferredCredential.Status)" IsPill="true" />
    </div>
}

<div class="mt-1">
    <RadzenCardLoader Variant="Variant.Filled" IsLoading=@deferredCredentialsState.Value.IsLoading>
        <RadzenStack>
            <RadzenFormField Text="@Global.TransactionId" Variant=Variant.Outlined>
                <RadzenTextBox Value="@deferredCredentialsState.Value.DeferredCredential.TransactionId"></RadzenTextBox>
            </RadzenFormField>
            @foreach (var claim in Claims)
            {
                <RadzenFormField Text="@claim.Name" Variant=Variant.Outlined>
                    <RadzenTextBox Name="@claim.Key" @bind-Value="@claim.Value"></RadzenTextBox>
                </RadzenFormField>
            }
        </RadzenStack>

        @if (deferredCredentialsState.Value.DeferredCredential != null && deferredCredentialsState.Value.DeferredCredential.Status == DeferredCredentialStatus.PENDING)
        {
            <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@Global.Update" Click="@(args => Update())" />
        }
    </RadzenCardLoader>
</div>

@code {
    [Parameter]
    public string id { get; set; } = null!;

    public List<EditableClaim> Claims { get; set; } = new List<EditableClaim>();

    public record EditableClaim
    {
        public string Name { get; set; }
        public string Key { get; set; }
        public string Value { get; set; }
    }

    RadzenDataGrid<DeferredCredential> deferredCredentialsGrid;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        SubscribeToAction<GetDeferredCredentialSuccessAction>((act) =>
        {
            var culture = requestLocalizationOptions.Value.DefaultRequestCulture.Culture;
            if (act.DeferredCredential.Status == DeferredCredentialStatus.PENDING)
            {
                Claims = act.DeferredCredential.Configuration.Claims.Select(c =>
                {
                    var translation = c.Translations.FirstOrDefault(t => t.Locale == culture.Name);
                    return new EditableClaim
                        {
                            Name = translation == null ? c.Name : translation.Name,
                            Key = c.SourceUserClaimName,
                            Value = string.Empty
                        };
                }).ToList();
            }
            else
            {
                Claims = act.DeferredCredential.Claims.Select(c =>
                {
                    var translation = act.DeferredCredential.Configuration.Claims.First(cl => cl.Name == c.Name).Translations.FirstOrDefault(t => t.Locale == culture.Name);
                    return new EditableClaim
                    {
                        Name = translation == null ? c.Name : translation.Name,
                        Value = c.Value
                    };
                }).ToList();
            }
        });
        SubscribeToAction<IssueDeferredCredentialSuccessAction>((act) =>
        {
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.DeferredCredentialIssued });
            StateHasChanged();
        });
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (string.IsNullOrWhiteSpace(id)) return;
        dispatcher.Dispatch(new GetDeferredCredentialAction { Id = id });
    }

    protected void Update()
    {
        dispatcher.Dispatch(new IssueDeferredCredentialAction { Claims = Claims.ToDictionary(c => c.Key, c => c.Value), Id = id });
    }
}